#include "qformlistwidget.h"

#include "widgethost.h"
#include "qformwidget.h"

#include "../../libs/sharedLib/qabstracthost.h"
#include "../../libs/sharedLib/qprojectcore.h"
#include "../../libs/sharedLib/qpagemanager.h"
#include "../../libs/sharedLib/qabstractproperty.h"
#include "../../libs/sharedLib/xml.h"
#include "../../libs/sharedLib/qpageinfo.h"
#include "../../libs/coreLib/qsoftcore.h"
#include "../../libs/sharedLib/qcommonstruct.h"

#include <QVBoxLayout>
#include <QMessageBox>
#include <QVariant>
#include <QApplication>
#include <QClipboard>

QFormListWidget::QFormListWidget(QWidget *parent):
    QWidget(parent)
{
    QVBoxLayout *l=new QVBoxLayout;
    l->setMargin(0);
    l->setSpacing(0);
    this->setLayout(l);

    this->setFocusPolicy(Qt::StrongFocus);

    SHAREDLIB::QProjectCore *c=SHAREDLIB::QProjectCore::getCore();
    connect(c,SIGNAL(projectOpen()),this,SLOT(projectOpen()));
    connect(c,SIGNAL(projectClosed()),this,SLOT(projectClosed()));
    connect(c,SIGNAL(projectSave()),this,SLOT(projectSave()));


    CORELIB::QSoftCore *core=CORELIB::QSoftCore::getCore();
    QAction *ac;
    ac=core->action("FormManager-Copy");
    if(ac!=NULL)
    {
        ac->setShortcut(QKeySequence::Copy);
        m_form_action_list.append(ac);
        connect(ac,SIGNAL(triggered()),this,SLOT(copy()));
    }

    ac=core->action("FormManager-Cut");
    if(ac!=NULL)
    {
        ac->setShortcut(QKeySequence::Cut);
        m_form_action_list.append(ac);
        connect(ac,SIGNAL(triggered()),this,SLOT(cut()));
    }

    ac=core->action("FormManager-Paste");
    if(ac!=NULL)
    {
        ac->setShortcut(QKeySequence::Paste);
        m_form_action_list.append(ac);
        connect(ac,SIGNAL(triggered()),this,SLOT(paste()));
        clipboard_changed();
    }

    ac=core->action("FormManager-Delete");
    if(ac!=NULL)
    {
        connect(ac,SIGNAL(triggered()),this,SLOT(del()));
        m_form_action_list.append(ac);
        ac->setShortcut(QKeySequence::Delete);
    }

    ac=core->action("FormManager-Select-All");
    if(ac!=NULL)
    {
        connect(ac,SIGNAL(triggered()),this,SLOT(selectAll()));
        m_form_action_list.append(ac);
        ac->setShortcut(QKeySequence::SelectAll);
    }

    connect(qApp->clipboard(),SIGNAL(changed(QClipboard::Mode)),this,SLOT(clipboard_changed()));
}

void QFormListWidget::addForm(SHAREDLIB::QPageInfo *info)
{
    if(m_infoToForm.value(info)!=NULL)
    {
        return;
    }
    WidgetHost *host=new WidgetHost(this);
    QFormWidget *form=new QFormWidget(info->get_type(),this);
    if(form->isNull())
    {
        delete host;
        delete form;
        return;
    }
    connect(form,SIGNAL(select(QWidget*)),this,SIGNAL(selectWidget(QWidget*)));
    connect(form,SIGNAL(select_changed()),this,SLOT(select_changed()));
    connect(form,SIGNAL(need_make_page_position()),this,SIGNAL(need_make_page_position()));
    form->setWidgetHost(host);
    form->setName(info->get_name());
    form->load(info->get_data(),info->get_type());
    host->setFormWidget(form);
    layout()->addWidget(host);
    host->setVisible(false);
    form->setProperty("objectName",info->get_name());
    m_formlist.append(host);
    form->setDefaultValue();
    m_formToInfo.insert(form,info);
    m_infoToForm.insert(info,form);
    emit formAddSignal(form);
    connect(form,SIGNAL(widgetAdd(QWidget*,int)),this,SIGNAL(formAddWidget(QWidget*,int)));
    connect(form,SIGNAL(widgetRemove(QWidget*)),this,SIGNAL(widgetRemove(QWidget*)));
}

void QFormListWidget::showForm(SHAREDLIB::QPageInfo *info)
{
    if(info==m_current)
    {
        return;
    }
    QFormWidget *newform=m_infoToForm.value(info);
    QFormWidget *current=m_infoToForm.value(m_current);
    m_current=info;
    if(current!=NULL)
    {
        foreach(QAction* ac,m_form_action_list)
        {
            current->removeAction(ac);
        }

        current->widgetHost()->setVisible(false);
    }
    if(newform!=NULL)
    {
        newform->widgetHost()->setVisible(true);
        newform->setFocus();
        newform->selectThis();
        foreach(QAction* ac,m_form_action_list)
        {
            newform->addAction(ac);
        }
    }
    emit showFormSignal(newform);
}

QFormWidget *QFormListWidget::currentForm()
{
    return m_infoToForm.value(m_current,NULL);
}

void QFormListWidget::projectOpen()
{
    clear();
    SHAREDLIB::QPageManager *p=SHAREDLIB::QProjectCore::getCore()->pageManager();

    int count=p->pageCount();
    for(int i=0;i<count;i++)
    {
        addForm(p->getPage(i));
    }

    showForm(p->getCurrentPage());

    connect(p,SIGNAL(addPageSignal(SHAREDLIB::QPageInfo*)),this,SLOT(addForm(SHAREDLIB::QPageInfo*)));
}

void QFormListWidget::projectClosed()
{
    SHAREDLIB::QPageManager *p=SHAREDLIB::QProjectCore::getCore()->pageManager();
    disconnect(p,SIGNAL(addPageSignal(SHAREDLIB::QPageInfo*)),this,SLOT(addForm(SHAREDLIB::QPageInfo*)));
}

void QFormListWidget::clear()
{
    emit selectWidget(NULL);
    emit showFormSignal(NULL);
    qDeleteAll(m_formlist);
    m_formlist.clear();
    m_infoToForm.clear();
    m_formToInfo.clear();
    m_current=NULL;
}

void QFormListWidget::projectSave()
{
    QMapIterator<QFormWidget*,SHAREDLIB::QPageInfo*> it(m_formToInfo);
    while(it.hasNext())
    {
        it.next();
        it.value()->set_data(it.key()->get_ui_string());
    }
}

void QFormListWidget::refreshForm()
{
}

void QFormListWidget::removeForm(SHAREDLIB::QPageInfo *info)
{
    QFormWidget *wid=m_infoToForm.value(info);
    if(wid!=NULL)
    {
        m_formlist.removeAll(wid->widgetHost());
        m_infoToForm.remove(info);
        m_formToInfo.remove(wid);
        delete wid->widgetHost();
    }
}

void QFormListWidget::left()
{
    QFormWidget* form=currentForm();
    if(form!=NULL)
    {
        form->left();
    }
}

void QFormListWidget::h_center()
{
    QFormWidget* form=currentForm();
    if(form!=NULL)
    {
        form->h_center();
    }
}

void QFormListWidget::right()
{
    QFormWidget* form=currentForm();
    if(form!=NULL)
    {
        form->right();
    }
}

void QFormListWidget::top()
{
    QFormWidget* form=currentForm();
    if(form!=NULL)
    {
        form->top();
    }
}

void QFormListWidget::v_center()
{
    QFormWidget* form=currentForm();
    if(form!=NULL)
    {
        form->v_center();
    }
}

void QFormListWidget::bottom()
{
    QFormWidget* form=currentForm();
    if(form!=NULL)
    {
        form->bottom();
    }
}

void QFormListWidget::sameWidth()
{
    QFormWidget* form=currentForm();
    if(form!=NULL)
    {
        form->sameWidth();
    }
}

void QFormListWidget::sameHeight()
{
    QFormWidget* form=currentForm();
    if(form!=NULL)
    {
        form->sameHeight();
    }
}

void QFormListWidget::sameGemotry()
{
    QFormWidget* form=currentForm();
    if(form!=NULL)
    {
        form->sameGemotry();
    }
}

SHAREDLIB::QPageInfo* QFormListWidget::pageInfo(QFormWidget *wid)
{
    return m_formToInfo.value(wid);
}

void QFormListWidget::copy()
{
    QFormWidget *form=currentForm();
    if(form!=NULL)
    {
        form->copy();
    }
}

void QFormListWidget::cut()
{
    QFormWidget *form=currentForm();
    if(form!=NULL)
    {
        form->cut();
    }
}

void QFormListWidget::paste()
{
    QFormWidget *form=currentForm();
    if(form!=NULL)
    {
        form->paste();
    }
}

void QFormListWidget::del()
{
    QFormWidget *form=currentForm();
    if(form!=NULL)
    {
        form->deleteSelectWidget();
    }
}

void QFormListWidget::selectAll()
{
    QFormWidget *form=currentForm();
    if(form!=NULL)
    {
        form->selectAll();
    }
}

void QFormListWidget::select_changed()
{
    QFormWidget *form=currentForm();
    int count=0;
    if(form!=NULL)
    {
        count=form->get_select_count();
    }
    QAction* ac=CORELIB::QSoftCore::getCore()->action("FormManager-Copy");
    if(ac!=NULL)
    {
        ac->setEnabled(count!=0);
    }
    ac=CORELIB::QSoftCore::getCore()->action("FormManager-Cut");
    if(ac!=NULL)
    {
        ac->setEnabled(count!=0);
    }
    ac=CORELIB::QSoftCore::getCore()->action("FormManager-Delete");
    if(ac!=NULL)
    {
        ac->setEnabled(count!=0);
    }
}

void QFormListWidget::clipboard_changed()
{
    QString text=qApp->clipboard()->text();

    SHAREDLIB::XMLObject xml;
    bool enabled=xml.read(text);
    QAction* ac=CORELIB::QSoftCore::getCore()->action("FormManager-Paste");
    if(ac!=NULL)
    {
        ac->setEnabled(enabled);
    }
}

void QFormListWidget::make_page_position(QVariant &param)
{
    tagPagePositionInfo info=param.value<tagPagePositionInfo>();
    if(m_current!=NULL)
    {
        info.m_data.insert("QFormListWidget::page_name",m_current->get_name());
        info.m_data.insert("QFormListWidget::page_type",m_current->get_type());
        param.setValue<tagPagePositionInfo>(info);
        QFormWidget *form=m_infoToForm.value(m_current);
        form->make_page_position(param);
    }
}

void QFormListWidget::set_page_position(const QVariant &param)
{
    if(m_current!=NULL)
    {
        QFormWidget *form=m_infoToForm.value(m_current);
        form->set_page_position(param);
    }
}

bool QFormListWidget::page_position(const QVariant &param)
{
    tagPagePositionInfo info=param.value<tagPagePositionInfo>();
    QString form_name=info.m_data.value("QFormListWidget::page_name").toString();
    QString form_type=info.m_data.value("QFormListWidget::page_type").toString();
    SHAREDLIB::QPageManager *p=SHAREDLIB::QProjectCore::getCore()->pageManager();
    SHAREDLIB::QPageInfo* in=p->getPage(form_type,form_name);
    int ret=false;
    if(m_current!=in)
    {
        showForm(in);
        ret=true;
    }

    if(m_current!=NULL)
    {
        QFormWidget *form=m_infoToForm.value(m_current);
        if(form->page_position(param))
        {
            ret=true;
        }
    }

    return ret;
}
